package com.leetcode.basic.map;

import java.util.*;
import java.util.concurrent.ConcurrentHashMap;

public class HashTable与ConcurrentHashMap性能 {
    static         List<String> keys   = new ArrayList<>();
    static         List<Object> values = new ArrayList<>();
    private static int          size;

    public static void main(String[] args) throws InterruptedException {
        // 造数据
        for (int i = 0; i < 999999; i++) {
            String key = String.valueOf(i);
            keys.add(key);
            values.add(i);

            key.hashCode();
            Integer.valueOf(i).hashCode();
        }

        size = Math.min(keys.size(), values.size());
        System.out.println("size=" + size);

        Map<String,Object> map = new HashMap<>();
//        Map<String, Object> map = new ConcurrentHashMap<>();
//        Map<String,Object> map = new Hashtable<>();

        Runnable task = () -> {
            execute(map);
        };
        new Thread(task).start();
        new Thread(task).start();
        new Thread(task).start();

        Thread.sleep(2000);
//        System.out.println();
    }

    private static void execute(Map<String, Object> map) {
        long begin = System.nanoTime();

        for (int i = 0; i < size; i++) {
            map.put(keys.get(i), values.get(i));
            map.put(keys.get(i), values.get(i));
            map.put(keys.get(i), values.get(i));
            map.put(keys.get(i), values.get(i));
        }

        for (int i = 0; i < size; i++) {
            map.get(keys.get(i));
            map.get(keys.get(i));
            map.get(keys.get(i));
            map.get(keys.get(i));
        }

        long end = System.nanoTime();

        System.out.println(map.getClass());
        System.out.println("time: " + (end - begin));
    }
}
